﻿
using System;
using System.Linq;

using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;

using Fuse8.ContentManagementFramework.Domain.Commands;
using Fuse8.ContentManagementFramework.Domain;

namespace Fuse8.ContentManagementFramework.DataAccess.SqlAccess.Commands
{
    internal class CmsUpdatePageControlDataCommand : CmsUpdatePageControlCommand
    {
        public override IQueryable<CmsPageControl> Execute()
        {
            var factory = DependencyServiceLocator.Current.GetInstance<IUnitOfWorkFactory<CmsPageControl>>();

            var currentUnitOfWork = factory.Get(false) as SqlUnitOfWork<CmsPageControl>;

            if (currentUnitOfWork != null)
            {
                var mapper = DependencyServiceLocator.Current.GetInstance<ISqlEntityDataMapper<CmsPageControl, int>>();

                SqlUtility<CmsPageControl, int>.ExecuteNonQuery(
                        currentUnitOfWork.Connection,
                        currentUnitOfWork.Transaction,
                        "cms_sp_PageControlSave",
                        new
                        {
                            PageControlId = CommandParameter.Id,
                            SiteGroupId = CommandParameter.SiteGroupId,
                            SiteId = CommandParameter.SiteId,
                            SystemLanguageId = CommandParameter.SystemLanguageId,
                            PageControlContentId = CommandParameter.PageControlContentId,
                            ModuleId = CommandParameter.ModuleId,
                            PageControlContent = CommandParameter.PageControlContent,
                            PageControlViewValue = CommandParameter.PageControlViewValue,
                            PageControlModelValue = CommandParameter.PageControlModelValue,
                            LastUpdatedBy = CommandParameter.LastUpdatedBy,
                            DateLastUpdated = CommandParameter.DateLastUpdated,
                            ContentTypeParameter = "Content", //TO DO - remove hardcoded value,
                            PageContentTypeParameter = CmsContentType.PageContentSystemContentType,
                            PageControlTypeParameter = "PageControl",
                            PageControlOrder = CommandParameter.PageControlOrder,
                            CommandParameter.AdminCaption
                        },
                        mapper);
            }

            return null;
        }
    }
}
